package com.wangyang.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangyang.mapper.ArticleMapper;
import com.wangyang.mapper.CategoryMapper;
import com.wangyang.pojo.Article;
import com.wangyang.pojo.Category;
import com.wangyang.pojo.Result;
import com.wangyang.service.ArticleService;
import com.wangyang.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public Result addArticle(Article article) {
        //获取当前登录用户id
        Integer uid = ThreadLocalUtil.get();
        article.setCreateUser(uid);
        //获取分类，判断分类是否合法
        Category category = categoryMapper.getCategoryById(article.getCategoryId());
        if(category==null||category.getCreateUser()!=uid){
            return Result.error("分类信息不存在或无权限使用该分类信息");
        }
        //添加文章
        articleMapper.addArticle(article);
        return Result.success();
    }

    @Override
    public Result getArticlePage(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
        //获取当前登录用户id
        Integer uid = ThreadLocalUtil.get();
        //PageHepler设置页码和页大小
        PageHelper.startPage(pageNum, pageSize);
        //条件查询list
        List<Article> rlist = articleMapper.getAriticleByCreateuser(uid, categoryId, state);
        //使用rlist生成PageInfo
        PageInfo<Article> pageInfo = new PageInfo<>(rlist);
        Map map = new HashMap();
        map.put("total", pageInfo.getTotal());
        map.put("items", pageInfo.getList());
        return Result.success(map);
    }

    @Override
    public Result getArticleById(Integer id) {
        //获取当前登录用户id
        Integer uid = ThreadLocalUtil.get();
        //获取文章根据文章id
        Article article = articleMapper.getArticleyById(id);
        //权限判断
        if(article==null||article.getCreateUser()!=uid){
            return Result.error("文章不存在或无权限查看该文章");
        }
        return Result.success(article);
    }

    @Override
    public Result updateArticle(Article article) {
        //获取当前登录用户id
        Integer uid = ThreadLocalUtil.get();
        //获取文章根据文章id
        Article articleq = articleMapper.getArticleyById(article.getId());
        //权限判断
        if(articleq==null||articleq.getCreateUser()!=uid){
            return Result.error("文章不存在或无权限修改该文章");
        }
        articleMapper.updateArticle(article);
        return Result.success();
    }

    @Override
    public Result deleteArticle(Integer id) {
        //获取当前登录用户id
        Integer uid = ThreadLocalUtil.get();
        //获取文章根据文章id
        Article article = articleMapper.getArticleyById(id);
        //权限判断
        if(article.getCreateUser()!=uid){
            return Result.error("文章不存在或无权限删除该文章");
        }
        articleMapper.deleteArticleById(id);
        return Result.success();
    }
}
